import filterData from '@/common/filterData';
import mergeData from '@/common/mergeData';
import formatter from '@/common/formatter';

const base = { title: '宽带开通总体情况' }
const dataset = {
  source: [
    ['日期', '派单', '归档', '在途', ' 在途归档比'],
    ['2023/12/18', 136, 192, 189, 0.36]
  ]
};

function getChartData([dataPool1, dataPool2], condition, mergeDepartment = ['全市']) {
  const collection = ['kuandai_xp', 'kuandai_gd', 'kuandai_zt'];
  const kt_ztgdb = 'kuandai_zaituguidangbi';
  const SheetHeader = ['派单', '归档', '在途'];
  condition = condition && condition.length ? [...condition[0], ...condition[1]] : undefined;
  const sheets = collection.map((coll, collIndex) => {
    const collectionData = dataPool1[coll];

    const allBiz = filterData.countDate(collectionData.all, condition);

    const sheet = [['日期', SheetHeader[collIndex]]]
    const allBizMerge = sheet.concat(filterData.countDate(allBiz, void 0, true));

    return allBizMerge;
  });

  const ztgdbSheet = dataPool2[kt_ztgdb];
  const averageData = ztgdbSheet.map(row => {
    const average = mergeDepartment.reduce((sum, condit) => {
      return sum + row[condit]
    }, 0) / mergeDepartment.length;
    return [row['日期'], average];
  })
  const zbtdbData = [['日期', '在途归档比']].concat(averageData)
  sheets.push(zbtdbData)

  const data = mergeData.mergeSheets(sheets);
  this.dataset.source = data;

  // 日期类
  if (data.length > 7) {
    this.dataZoom[0].show = true;
    this.dataZoom[0].startValue = data.length - 8;
    this.dataZoom[0].labelFormatter = (value) => formatter.excelDate(data[value + 1][0]);
  }

}
const seriesTemplate = [['bar', 'bar', 'bar'], ['line']];
// ======更新内容↑========
const series = seriesTemplate.flatMap((arr, index) =>
  arr.map(type => ({ type, yAxisIndex: index }))
);

export default {
  title: {
    text: base.title,
    left: 'center'
  },
  legend: {
    type: 'scroll',
    top: 30
  },
  grid: {
    width: '96%',
    height: '80%',
    left: '2%',
    right: '2%',
    top: '16%',
    bottom: '2%',
    containLabel: true
  },
  tooltip: {
    trigger: window.isMobile ? 'item' : 'axis',
    axisPointer: {
      type: 'cross',
      crossStyle: {
        color: '#999'
      }
    },
    formatter: (params) => {
      if (window.isMobile) {
        const value = params.value[params.dimensionNames.indexOf(params.seriesName)];
        let tip = `<div class="space-y-1"><div class='font-bold'>${formatter.excelDate(params.name)}</div>`;
        tip += `<div class='flex justify-between space-x-6 p-0'>
        <span>${params.marker + params.seriesName}</span>
        <span class="font-bold">${value ? formatter.valueToText(value) : '-'}</span>
        </div>`
        tip += '</div>'
        return tip
      } else {
        let tip = `<div class="space-y-1"><div class='font-bold'>${formatter.excelDate(params[0].name)}</div>`;
        params.forEach((item, index) => {
          const value = item.value[item.dimensionNames.indexOf(item.seriesName)];
          tip += `<div class='flex justify-between space-x-6 p-0'>
        <span>${item.marker + item.seriesName}</span>
        <span class="font-bold">${value ? formatter.valueToText(value) : '-'}</span>
        </div>`
        });
        tip += '</div>'
        return tip
      }
    }
  },
  dataset,
  dataZoom: [
    { show: false }
  ],
  xAxis: {
    type: 'category',
    axisPointer: {
      type: 'shadow',
      label: {
        formatter: (params) => formatter.excelDate(params.value)
      }
    },
    axisLabel: {
      formatter: (value) => formatter.excelDate(value)
    }
  },
  yAxis: [
    {
      type: 'value',
      name: ''

    }, {
      type: 'value',
      name: '',
      axisLabel: {
        formatter: (value) => `${(value * 100).toFixed(2)}`
      }
    }
  ],
  series,
  getChartData,
}